package com.hst.web.sys.proxy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.Page;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.core.meta.proxies.OREntityUtils;
import com.hst.web.base.entity.TAccountInfo;
import com.hst.web.common.utils.ConstantUtil;
import com.hst.web.sys.entity.TUser;
import com.hst.web.sys.entity.TUserRole;
import com.hst.web.sys.service.UserService;
import com.hst.web.sys.ui.User;

public class UserProxy extends DefaultDataProxy{
	
	@Autowired
	OREntityUtils orUtil;
	
	@Autowired
	ISQLDao sql;
	
	@Autowired
	UserService userService;
	
	@Override
	public Page<?> list(Map<String, ?> query, int pageIdx) {
		Page<?> p = sql.list("user.listUsers",query, pageIdx, Page.getPageSize());
		return p;
	}

	@Override
	public List<?> list(Map<String, ?> query) {
		List<ORQuery> querys = parseQuery(query);
		return convert2MetaData(dao.list(getEntityClass(), querys));
	}
	
	@Override
	public Object save(MetaData data) throws Exception {
		User curUser = (User) data;
		Object data1 = convert2Entity(data);
		List<ORQuery> querys = new ArrayList<ORQuery>();
		
		Encoder encoder = Base64.getEncoder();
		if (1 == data.getOp()) {
			TUser u = (TUser) data1;
			querys.add(new ORQuery(Op.eq,"operatorNbr",u.getOperatorNbr()));
			List<TUser> users = dao.list(TUser.class,querys);
			if(users.size() > 0) {
				throw new Exception("账号重复");
			}
			u.setPassword(new String(encoder.encode(u.getPassword().getBytes("UTF-8"))));
			u.setUserTypeOld(ConstantUtil.USETYPEMAP.get(curUser.getRole()));
			dao.save(u);
		} else {
			Object data2 = null;
			Serializable pk = (Serializable) orUtil.getEntityPk(data1);
			if (null != pk)
				data2 = dao.get(getEntityClass(), pk);
			TUser u = (TUser) data1;
			String employeeCode = u.getEmployeeCode();
			querys.add(new ORQuery(Op.eq,"operatorNbr",u.getOperatorNbr()));
			querys.add(new ORQuery(Op.neq,"id",u.getId()));
			List<TUser> users = dao.list(TUser.class,querys);
			
			if(users.size() > 0) {
				throw new Exception("账号重复");
			}
			u.setPassword(new String(encoder.encode(u.getPassword().getBytes("UTF-8"))));
			u.setUserTypeOld(ConstantUtil.USETYPEMAP.get(curUser.getRole()));
			if (null == data2)
				dao.save(u);
			else {
				BeanUtils.copyProperties(data2, u);
				dao.update(data2);
			}
			
			//同步更新TAccountInfo表的password字段
			String userName = userService.vaildateAccountIdentity(employeeCode);
			if (StringUtils.isNotEmpty(userName)) {
				TAccountInfo accountInfo = dao.get(TAccountInfo.class, userName);
				if (accountInfo != null) {
					accountInfo.setPassword(u.getPassword());
					dao.update(accountInfo);
				}
			}
		}
		
		User u = (User) data;
		User user = (User) convert2MetaData(data1);
		user.setRole(u.getRole());
		user.setCompanyCode(u.getCompanyCode());
		user.setContractorCode(u.getContractorCode());
		
		if("9".equals(u.getUserType())){ //是否需要修改角色
			querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq,"userid",u.getId()));
			List<TUserRole> rels = dao.list(TUserRole.class,querys);
			if(rels.size() > 0) {
				TUserRole rel = rels.get(0);
				if(!rel.getRoleid().equals(user.getRole())) {
					dao.delete(rel);
					TUserRole relNew = new TUserRole();
					relNew.setUserid(user.getId());
					relNew.setRoleid(user.getRole());
					dao.save(relNew);
				}
			}else {
				TUserRole rel = new TUserRole();
				rel.setUserid(user.getId());
				rel.setRoleid(user.getRole());
				dao.save(rel);
			}
		}
		return user;
	}
}
